package com.atguigu.yygh.hosp.testmongo;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.regex.Pattern;

@Api(tags = "Mongo")
@RestController
@RequestMapping("/mongo")
public class MongoTemplateController {
    @Autowired
    private MongoTemplate mongoTemplate;

    //添加
    @ApiOperation("添加")
    @GetMapping("create")
    public void createUser() {
        User user = new User();
        user.setAge(20);
        user.setName("test");
        user.setEmail("111@qq.com");
        User u = mongoTemplate.insert(user);
        System.out.println("u = " + u);
    }

    //查询所有操作
    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public void findAll() {
        //如果实体类注解了去哪个集合查,这里可以只传实体类的模板
        List<User> all = mongoTemplate.findAll(User.class);
        System.out.println(all);
    }

    //根据id查询
    @GetMapping("getById")
    public void getById() {
        User user = mongoTemplate.findById("612f25e653d5b3f14028cd61", User.class);
        System.out.println(user);
    }

    //条件查询数据
    @ApiOperation("条件查询")
    @GetMapping("findQuery")
    public void findQuery() {
        Query query = new Query(Criteria.where("name").is("test").and("age").is(20));
        List<User> userList = mongoTemplate.find(query, User.class);
        System.out.println("userList = " + userList);
    }

    //模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("findLike")
    public void findLike(){
        String name = "sa";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("name").regex(pattern));
        List<User> userList = mongoTemplate.find(query, User.class);
        System.out.println(userList);
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("findPage")
    public void findPage(){
        String name = "sa";
        int pageNo = 1;
        int pageSize = 2;
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

        //同new Query(Criteria.where("name").regex(pattern));
        Query query = new Query();
        query.addCriteria(Criteria.where("name").regex(pattern));

        //查询记录数
        long count = mongoTemplate.count(query, User.class);
        //分页查询数据集合
        List<User> userList = mongoTemplate.find(query.skip((pageNo-1)*pageSize).limit(pageSize), User.class);

        System.out.println("userList = " + userList);
        System.out.println("count = " + count);
    }

    //修改操作
    @ApiOperation("修改")
    @GetMapping("update")
    public void update(){
        //根据id查询
        User user = mongoTemplate.findById("6130b5fd9c64ec16a4459b9f", User.class);
        //设置修改值 创建update 设置修改条件 根据id修改
        user.setName("wangjian_update");
        user.setAge(20);
        user.setEmail("6666@qq.com");
        Update update = new Update();
        update.set("name",user.getName());
        update.set("age",user.getAge());
        update.set("email",user.getEmail());
        Query query = new Query(Criteria.where("_id").is(user.getId()));
        //调用方法实现
        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
        long modifiedCount = upsert.getModifiedCount();
        System.out.println(modifiedCount);
    }

    @ApiOperation("删除")
    @GetMapping("delete")
    public void delete(){
        Query query = new Query(Criteria.where("_id").is("6130b5fd9c64ec16a4459b9f"));
        DeleteResult remove = mongoTemplate.remove(query, User.class);
        long deletedCount = remove.getDeletedCount();
        System.out.println("deletedCount = " + deletedCount);
    }

}
